package com.testDemo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * Redis简单操作
 * @author AidenJ
 *
 */
public class Demo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Jedis jedis = null;
		//连接redis服务
		try {
			jedis = new Jedis("127.0.0.1",6379);
		
		/*@SuppressWarnings("resource")
		Jedis jedis = new Jedis("192.168.0.169",6379);*/
		
		//密码验证-如果你没有设置redis密码可不验证即可使用相关命令
		//jedis.auth("youpassword");
		
		//简单的key-value 存储
		jedis.set("redis", "myredis");
		System.out.println(jedis.set("redis", "myredis"));
		System.out.println(jedis.get("redis"));
		
		//在原有值得基础上添加,如若之前没有该key，则导入该key
		//之前已经设定了redis对应"myredis",此句执行便会使redis对应"myredisyourredis"
		jedis.append("redis", ",yourredis");
		jedis.append("content", ",rabbit");
		jedis.append("contents", ",rabbits");
		System.out.println("redis:"+jedis.get("redis"));
		System.out.println("content:"+jedis.get("content"));
		System.out.println("contents:"+jedis.get("contents"));
		
		jedis.del("content");
		jedis.del("contents");
		System.out.println("========================contents:"+jedis.getSet("redis", "yourredisyourredisyourredisyourredis"));//修改前的数据
//		System.out.println(jedis.asking());//报错ERR This instance has cluster support disabled
		
		
		//mset 是设置多个key-value值   参数（key1,value1,key2,value2,...,keyn,valuen）
		//mget 是获取多个key所对应的value值  参数（key1,key2,key3,...,keyn）  返回的是个list
		jedis.mset("name1","yangw","name2","demon","name3","elena","name4","");
		System.out.println(jedis.mget("name1","name2","name3","name4"));
		System.out.println(jedis.mget("name2","name3"));
		jedis.set("name4", "123456");
		System.out.println(jedis.mget("name2","name4"));
		jedis.mset("name4", "654321");
		System.out.println(jedis.mget("name2","name4"));
		
		//map
		Map<String,String> user = new HashMap<String,String>();
		user.put("name", "cd");
		user.put("password", "123456");
		//map存入redis
		jedis.hmset("user", user);        /*仅支持Map<String,String>**/
		System.out.println("user→name:"+jedis.hmget("user", "name"));
		System.out.println("user:"+jedis.hmget("user", "name").size());
		//System.out.println(jedis.get("user"));
		/*
		Map<String,List<String>> mapList = new HashMap<String,List<String>>();
		List<String> listStr = new ArrayList<String>();
		listStr.add("aaa");
		listStr.add("bbb");
		listStr.add("ccc");
		
		mapList.put("listMap", listStr);
		jedis.hmset("listMap",mapList);
		List<String> list = jedis.hmget("listMap");
		for (String string : list) {
			System.out.println(string);
		}
		*/
		
		//mapkey个数
		System.out.println(String.format("len:%d", jedis.hlen("user")));
		
		//map中的所有键值
		System.out.println(String.format("keys: %s", jedis.hkeys("user") ));
		
		//map中的所有value
		System.out.println(String.format("values: %s", jedis.hvals("user") ));
		for (String string : jedis.hvals("user")) {
			System.out.println(string);
		}
		
		//取出map中的name字段值
		List<String> rsmap = jedis.hmget("user", "name","password");
		System.out.println("取出map中的name字段值："+rsmap);
		
		//删除map中的某一个键值 password
		jedis.hdel("user", "password");
		System.out.println(jedis.hmget("user", "name", "password"));
		
		//list
		jedis.del("listDemo");
		//第一个是key，第二个是起始位置，第三个是结束位置，jedis.llen获取长度 -1表示取得所有 
		System.out.println(jedis.lrange("listDemo", 0, 1));
		//向list中添加三个参数
		jedis.lpush("listDemo", "A");
		jedis.lpush("listDemo", "B");
		jedis.lpush("listDemo", "C");//左边插入
		jedis.rpush("listDemo", "D");//右边插入
//		System.out.println("=================================="+jedis.lpop("listDemo"));
		System.out.println(jedis.lrange("listDemo", 3,3));
		System.out.println(jedis.lrange("listDemo", 0, -1));
		
		
		jedis.hset("1", "3", "3");
		
		//set
		jedis.sadd("sname", "wobby");
		jedis.sadd("sname", "kings");
		jedis.sadd("sname", "demon");
		System.out.println(jedis.scard("sname"));//参数长度
		System.out.println(jedis.smembers("sname"));//返回所有参数
		System.out.println(jedis.sismember("sname", "wobby"));//判断参数是否存在
		System.out.println(jedis.srandmember("sname"));//随机返回参数
		//删除一个对象
		jedis.srem("sname", "demon");
		System.out.println(jedis.smembers("sname"));
		} catch (JedisConnectionException e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("连接失败");
		}
		
		/**
		 * 累加 ：incr 只能是数字整型
		 */
		jedis.set("loginNumber", jedis.incr("loginNumber").toString());
		System.out.println(jedis.get("loginNumber"));
		jedis.del("loginNumber");
		/**
		 * 文章的访问量（静态化的页面），
		 * 用户点击文章时，去后台redis中给文章+1，
		 * （定时器）每天凌晨判断redis中参数>0，去修改数据库中文章 的总访问量&归零今日访问量，并修改redis中总访问量，
		 * 总的访问量+今日访问量=现在访问量
		 */
		
		
		
		jedis.hset("1", "3", "3");
		jedis.hset("1", "2", "2");
		jedis.hset("1", "4", "4");
		System.out.println(jedis.hgetAll("1").keySet());
		//System.out.println(jedis.asking());
		/*jedis.lpush("1","3");
		jedis.lpush("1","2");
		jedis.lpush("1","4");
		/*System.out.println(jedis.hget("1", "3"));*/
		//System.out.println(jedis.lrange("1",0,1));
		
		/**
		 * 获取信息
		 */
		//System.out.println(jedis.info());
		String roomid1 = "room1";
		String roomid2 = "room2";
		jedis.set("Spring:"+roomid1+":springdate", "111111111");
		jedis.set("Spring:"+roomid2+":springdate", "222222222");
		jedis.set("Spring:"+roomid2+":userdate", "333333333");
		
		//jedis.set("Spring:springdate", "2222222");
		System.out.println(jedis.get("Spring:"+roomid1+":springdate"));
		System.out.println(jedis.keys("Spring:*:*"));
		Set<String> list = jedis.keys("Spring:*:*");
		/*for (String str : list) {
			System.out.println(jedis.get(str));
		}*/
		
		String[] keyArray = new String[list.size()];
		list.toArray(keyArray);
		System.out.println(jedis.mget(keyArray));
		
		/*Object[] liststr = jedis.keys("Spring:*:*").toArray();
		
		/*Set<String> listValue = jedis.sdiff("Spring:*:*");
		System.out.println(jedis.smembers("Spring"));
		List<String> jl= jedis.mget((String[]) liststr);
		System.out.println(jedis.mget((String[]) liststr));*/
		
		
		
		/*String[] keyArray = new String[list.size()];
		list.toArray(keyArray);*/
		//jedis.mget(list);
		
		/*List<byte[]> lstr = new ArrayList<>();
		byte[] bArray = new byte[list.size()];
//		lstr.addAll(bArray);
		lstr = jedis.mget(bArray);
		System.out.println(lstr);*/
		
//		System.out.println(jedis.mget(      ));
		//jedis.(liststr);
		/*Set<String> strSet = new HashSet<String>();
		strSet.add(list);*/
		
		/*byte[] keyArra = new byte[list.size()];
		list.toArray();*/
		//System.out.println(jedis.get(keyArra[0]));
		
	}
}
